package com.smartscreen.contentmanage.controller;

import com.smartscreen.contentmanage.entity.Resource;
import com.smartscreen.contentmanage.service.ResourceService;
import com.smartscreen.common.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/content/resources")
@Validated
public class ResourceController {
    
    @Autowired
    private ResourceService resourceService;
    
    /**
     * 上传资源文件
     * @param file 文件
     * @param description 描述
     * @param subjectId 学科ID
     * @param gradeId 年级ID
     * @return 上传结果
     */
    @PostMapping
    public ApiResponse<Map<String, Object>> uploadResource(
            @RequestParam("file")  MultipartFile file,
            @RequestParam("description")  String description,
            @RequestParam("subjectId")  String subjectId,
            @RequestParam("gradeId") String gradeId) {
        
        try {
            if (file == null || file.isEmpty()) {
                return ApiResponse.badRequest("上传文件不能为空");
            }
            
            if (file.getSize() > 50 * 1024 * 1024) { // 50MB限制
                return ApiResponse.badRequest("文件大小不能超过50MB");
            }
            
            // 这里应该实现文件上传逻辑，保存到文件服务器或云存储
            // 为了示例，我们模拟一个URL
            String fileUrl = "https://cdn.example.com/resources/" + System.currentTimeMillis() + "_" + file.getOriginalFilename();
            
            Resource resource = new Resource();
            resource.setFileName(file.getOriginalFilename());
            resource.setDescription(description);
            resource.setFileUrl(fileUrl);
            resource.setSubjectId(subjectId);
            resource.setGradeId(gradeId);
            resource.setFileSize(file.getSize());
            resource.setFileType(file.getContentType());
            
            boolean success = resourceService.save(resource);
            
            if (success) {
                Map<String, Object> data = new HashMap<>();
                data.put("resourceId", resource.getResourceId());
                data.put("fileName", resource.getFileName());
                data.put("url", resource.getFileUrl());
                return ApiResponse.created("上传成功", data);
            } else {
                return ApiResponse.serverError("上传失败");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("上传失败：" + e.getMessage());
        }
    }
    
    /**
     * 下载资源文件
     * @param resourceId 资源ID
     * @return 重定向到文件URL
     */
    @GetMapping("/{resourceId}/download")
    public ApiResponse<Map<String, String>> downloadResource(@PathVariable String resourceId) {
        try {
            if (resourceId == null || resourceId.trim().isEmpty()) {
                return ApiResponse.badRequest("资源ID不能为空");
            }
            
            Resource resource = resourceService.getById(resourceId);
            
            if (resource != null) {
                Map<String, String> data = new HashMap<>();
                data.put("downloadUrl", resource.getFileUrl());
                data.put("fileName", resource.getFileName());
                return ApiResponse.success("获取下载链接成功", data);
            } else {
                return ApiResponse.notFound("资源不存在");
            }
        } catch (Exception e) {
            return ApiResponse.serverError("获取下载链接失败：" + e.getMessage());
        }
    }
}